﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using ComLib.ComFun;

namespace ComLib
{
    public class IniConfigFile
    {
        const string m_strFileName = "Config.ini";
               
        /// <summary>
        ///  加载资源(先在当前目录查找，当前目录查找，向上一级BIN下查找)
        /// </summary>
        /// <param name="strRes">需要查找资源名称</param>
        /// <returns>查找成功的全路径文件名称</returns>
        public static string GetResources(string strRes)
        {
            string strAppPath = Paths.Directory;
            string strFile = strAppPath +"\\" + strRes;
            if (File.Exists(strFile))
                return strFile;             

            strFile = strAppPath.Substring(0, strAppPath.LastIndexOf("\\"));
            strFile = strFile + "\\Bin\\" + strRes;
            if (File.Exists(strFile))
                return strFile;
            else
                throw new Exception("查找资源失败：" + strRes);
        }

        #region 读取INI文件
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey,string iniFileName)
        {
            if (string.IsNullOrEmpty(iniFileName))
            {
                iniFileName = GetResources(m_strFileName);
            }            
          
            StringBuilder temp = new StringBuilder(500);
            //MessageBox.Show(iniFileName);
            int i = GetPrivateProfileString(Section, IniKey, "", temp, 500, iniFileName);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件(找不到配置文件返回默认值)
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey,string strDefault,string iniFileName)
        {
            if (string.IsNullOrEmpty(iniFileName))
            {
                iniFileName = GetResources(m_strFileName);
            }

            StringBuilder temp = new StringBuilder(500);
            //MessageBox.Show(iniFileName);
            int i = GetPrivateProfileString(Section, IniKey, strDefault, temp, 500, iniFileName);
            return temp.ToString();
        }

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        #region 写INI文件
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string Section, string IniKey, string NewValue,string iniFileName)
        {
            if (string.IsNullOrEmpty(iniFileName))
            {
                iniFileName = GetResources(m_strFileName);
            }
            long i = WritePrivateProfileString(Section, IniKey, NewValue, iniFileName);
            return i;
        }

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(
            string section, string key, string val, string filePath);
        #endregion
    }
}